

var curHangSelected = 0;
var curLoaiSelected = 0;

$(document).ready(function() {
    curHangSelected = 0;
    curLoaiSelected = 0;
    scrollToTop();
	if (loadCalendar) {
		$.datepicker.setDefaults({
			dateFormat: 'dd-M-yy',
			changeMonth: true,
			changeYear: true,
			maxDate: new Date(),
			minDate: new Date(2005, 1, 1),
			showButtonPanel: true
		});
		$("#startdate, #enddate").datepicker();
	}
	
	actSelectListHang();
	actSelectListLoai();
	clickBtnThemHang();
    onClickOnModalThemHang();
    
    clickBtnThemLoai();
    onClickOnModalThemLoai();
    clickBtnThemSanPham ();
    $('p.content-loai').hide();
    $('table.content-input-model-detail').hide();
});

function scrollToTop(){
    $(window).scroll(function(){
		if ($(this).scrollTop() > 100) {
			$('.scrollup').fadeIn();
		} else {
			$('.scrollup').fadeOut();
		}
	}); 
	
	$('.scrollup').click(function(){
		$("html, body").animate({ scrollTop: 0 }, 600);
		return false;
	});
}

function redirect(url) {
	window.location = url;
}

function loadingToggle(show) {
	if (show == true) {
		var w = $(window).width();
		var h = $(document).height();
		$("div#modal").css({
			"width": w,
			"height": h
		}).fadeIn();
	} else {
		$("div#modal").fadeOut();
	}
}

function ajaxHandle(url, data) {
    loadingToggle(true);
    $.ajax({
        url: url,
        method: "POST",
        data: data,
        success: function(ret) {
            onAjaxSuccess(url, ret);
            loadingToggle(false);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            onAjaxError(url, xhr, ajaxOptions, thrownError);
            loadingToggle(false);
        }
    });
}

function onAjaxSuccess(url, ret) {
    if (url.indexOf("manager/saveProductDetail") != -1) {
        if (ret != 1) {
            $('p.message').html(ret);
        }else{
            $('p.message').html("<span class='success'>Insert successful!</span>");
        }
    }
}

function onAjaxError(url, xhr, ajaxOptions, thrownError) {
    if (url.indexOf("manager/saveProductDetail") != -1) {
       $('p.message').html("<span class='error'>Them san pham that bai !</span>");
    }
}

function clickBtnThemLoai() {
   $(document).on('click', "button#btnaddproduct", function() {
        if($("button#btnaddproduct").text() == dong){
            $("button#btnaddproduct").text(themLoai);
            $("div#myModalAddProduct").addClass("hide").fadeOut();
        }else{
            $("button#btnaddproduct").text(dong);
            $("div#myModalAddProduct").removeClass("hide").unbind('fadeOut').fadeIn();
        }
    });
}

function clickBtnThemHang() {
   $(document).on('click', "button#btnaddhang", function() {
        if($("button#btnaddhang").text() == dong){
            $("button#btnaddhang").text(themLoai);
            $("div#myModalAddHang").addClass("hide").fadeOut();
        }else{
            $("button#btnaddhang").text(dong);
            $("div#myModalAddHang").removeClass("hide").unbind('fadeOut').fadeIn();
        }
    });
}

function clickBtnThemSanPham () {
    $(document).on('click', "button#btnaddproductdetail", function() {
        var data    = $('form#frm_input_product').serialize()
        var url     = $('form#frm_input_product').attr('action');
        ajaxHandle(url,data);
    });
}
function onClickOnModalThemLoai () {
    $(document).on('click', "div#myModalAddProduct button#btnok", function() {
        var name = $("div#myModalAddProduct input#name").val();
        doAddProduct(name);
    });

    $(document).on('click', "div#myModalAddProduct button#btncancel", function() {
        $("button#btnaddproduct").text(themLoai);
        $("div#myModalAddProduct").addClass("hide").fadeOut();
    });
}

function onClickOnModalThemHang () {
    $(document).on('click', "div#myModalAddHang button#btnok", function() {
        var name = $("div#myModalAddHang input#name").val();
        doAddHang(name);
    });

    $(document).on('click', "div#myModalAddHang button#btncancel", function() {
        $("button#btnaddhang").text(themLoai);
        $("div#myModalAddHang").addClass("hide").fadeOut();
    });
}

function doAddHang(name) {
    loadingToggle(true);
        $.ajax({
            url: $("input#urlmanager").val()+"/saveHang",
            method: "POST",
            data: "name=" + name,
            success: function(ret) {
                if (ret != 1) {
                    $('p.message').html(ret);
                }else{
                    $('p.message').html("<span class='success'>Insert successful!</span>");
                    $("button#btnaddproduct").text(themLoai);
                    $("div#myModalAddProduct").addClass("hide").fadeOut();
                    refreshHangList();
                }
                loadingToggle(false);
            },
            error: function(xhr, ajaxOptions, thrownError) {
                $('p.message').html("<span class='error'>Them  " + name + " that bai !</span>");
                loadingToggle(false);
            }
        });
}

function actSelectListHang () {
     $("select#cbxhang").change(function() {
        var hangId = $( this ).val();
        if (hangId >0) {
            curHangSelected = hangId;
            $('p.content-loai').show();
            $('table.content-input-model-detail').hide();
            refreshProductList(hangId);
        }else{
            $('p.content-loai').hide();
        }
     });
}
function actSelectListLoai () {
    var pathname = window.location.pathname;

     $("select#cbxmodel").change(function() {
         if (pathname.indexOf("manager/manager") == 1) {
            var loaiId = $( this ).val();
            if (loaiId >0) {
                curLoaiSelected = loaiId;
                $('table.content-input-model-detail').show();
            }else{
                $('table.content-input-model-detail').hide();
            }
         }else{
            var loaiId = $( this ).val();
            if (loaiId >0) {
                refreshModelDetailList(loaiId);
            }
         }
     });
}


function refreshHangList () {
    $.ajax({
            url: $("input#urlmanager").val()+"/getMComList",
            method: "POST",
            data:'',
            success: function(ret) {
                $("form#frm_input_product select#cbxhang > option").remove();
                $("form#frm_input_product select#cbxhang").append(ret);
            },
            error: function(xhr, ajaxOptions, thrownError) {
            }
        });
}

function doAddProduct (name) {
    loadingToggle(true);
        $.ajax({
            url: $("input#urlmanager").val()+"/saveProduct",
            method: "POST",
            data: "name=" + name + "&hangId="+curHangSelected,
            success: function(ret) {
                if (ret != 1) {
                    $('p.message').html(ret);
                }else{
                    $('p.message').html("<span class='success'>Insert successful!</span>");
                    $("button#btnaddproduct").text(themLoai);
                    $("div#myModalAddProduct").addClass("hide").fadeOut();
                    refreshProductList(curHangSelected);
                }
                loadingToggle(false);
            },
            error: function(xhr, ajaxOptions, thrownError) {
                $('p.message').html("<span class='error'>Them  " + name + " that bai !</span>");
                loadingToggle(false);
            }
        });
}

function refreshProductList(hangId) {
    $.ajax({
            url: $("input#urlmanager").val()+"/getProductList",
            method: "POST",
            data:"hangId=" + hangId,
            success: function(ret) {
                $("select#cbxmodel > option").remove();
                $("select#cbxmodel").append(ret);
            },
            error: function(xhr, ajaxOptions, thrownError) {
            }
        });
}

 function refreshModelDetailList(productId) {
    $.ajax({
            url: $("input#urlhome").val()+"/getProductDetailList",
            method: "POST",
            data:"product_id=" + productId,
            success: function(ret) {
                console.log(ret);
                $("div.contain_model_list").html(ret);
            },
            error: function(xhr, ajaxOptions, thrownError) {
            }
        });
 }
